package helipay.api.inteceptors;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;

import helipay.api.util.I18nUtil;
import helipay.api.service.BaseService;

public class Inteceptor implements HandlerInterceptor {

	private Log logger = LogFactory.getLog("REQUEST_PARAMETER_LOG");
	
	@Autowired
	private BaseService baseService;
	//@SuppressWarnings("rawtypes")
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//打印log
		try {

			Enumeration<?> names = request.getParameterNames();

			String uri = request.getRequestURI();
			StringBuilder logInfo = new StringBuilder();
			logInfo.append(uri).append("?");

			while (names.hasMoreElements()) {
				String name = (String) names.nextElement();
				if(name.equals("password")){
					
					logInfo.append(name).append("=").append("******").append("&");
				}else{
					String[] values = request.getParameterValues(name);
					
					logInfo.append(name).append("=").append(ararryToString(values)).append("&");
				}
			}

			logger.info(logInfo);

		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		//獲取spring mvc PathVariable里的值
		Object pathVariables = request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE); 
		
		String userId="";
		String session="";
		if(pathVariables instanceof Map<?,?>){
			userId=((Map<?,?>)(pathVariables)).get("userId")+"";
			session=((Map<?,?>)(pathVariables)).get("session")+"";
			logger.info("userId["+userId+"] ,session["+session+"]");
		}
		
		Assert.hasText(session, "session "+I18nUtil.getMessage(-2, null, null));
		Assert.hasText(userId, "userId "+I18nUtil.getMessage(-2, null, null));
		
		logger.info("userId["+userId+"] ,session["+session+"]");
		
//		if(!baseService.loginCheck(userId,session)){
//			throw new SessionException("系统繁忙，请稍后再试！");
//		}
		
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

	/**
	 * 功能描述：输出数组,枨式为[value1 ; value2 ; .......]
	 * 
	 * @param values
	 * @return
	 */
	private String ararryToString(String[] values) {
		if (values == null) {
			return null;
		}

		StringBuilder result = new StringBuilder();

		if (values.length > 1) {
			result.append("[");
		}

		for (String value : values) {

			String shortValue = value;
			if (result.length() > 0) {
				result.append(" ; ").append(shortValue);
			} else {
				result.append(shortValue);
			}
		}

		if (values.length > 1) {
			result.append("]");
		}

		return result.toString();
	}
}
